﻿@using System.ComponentModel
@using AntDesign.TableModels

<Table TItem="Data" DataSource="@data" OnRow="OnRow" OnHeaderRow="OnHeaderRow" @bind-SelectedRows="selectedData" RowKey="x=>x.Key">
  <PropertyColumn Property="c=>c.Name" OnCell="OnCell" OnHeaderCell="OnHeaderCell">
    <a>@context.Name</a>
  </PropertyColumn>
  <PropertyColumn Property="c=>c.Age"></PropertyColumn>
  <Column Title="Address" TData="string">
    <TitleTemplate>
      <span> <Icon Type="environment" /> Address </span>
    </TitleTemplate>
  </Column>
  <Column Title="Tag" TData="string">
    @foreach (var tag in context.Tags)
    {
      <Tag Color="@(tag == "loser" ? "volcano" : tag.Length > 5 ? "geekblue" : "green")">@tag</Tag>
    }
  </Column>
</Table>

@code {
    record Data(string Key, string Name, int Age, string Address, string[] Tags);

    IEnumerable<Data> selectedData= [];

    Data[] data = new Data[]
    {
        new("1", "John Brown",32, "New York No. 1 Lake Park",new[] {"nice", "developer"}),
        new("2", "Jim Green",42, "London No. 1 Lake Park",new[] { "loser"}),
        new("3", "Joe Black",32, "Sidney No. 1 Lake Park",new[] { "cool", "teacher" }),
    };

    Dictionary<string, object> OnRow(RowData<Data> row) => new()
    {
      ["id"] = row.Data.Key,
      ["onclick"] = () =>
      {
        Console.WriteLine($"row {row.Data.Key} was clicked");
        if (selectedData.Contains(row.Data))
        {
            selectedData = selectedData.Where(x => x.Key != row.Data.Key);
        }
        else
        {
            selectedData = selectedData.Concat(new[] { row.Data });
        }
          StateHasChanged();
      }
    };

    Dictionary<string, object> OnHeaderRow() => new()
    {
      ["id"] = "header-row",
      ["onclick"] = () =>
      {
          Console.WriteLine("header row was clicked");
      }
    };

    Dictionary<string, object> OnCell(CellData cellData) => new()
    {
      ["onclick"] = () =>
      {
          Console.WriteLine($"cell {((RowData<Data>)cellData.RowData).Data.Name} was clicked");
      }
    };

    Dictionary<string, object> OnHeaderCell() => new()
    {
      ["onclick"] = () =>
      {
          Console.WriteLine("header cell was clicked");
      }
    };
}